﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemplatesPrint.DBHelper;
using TemplatesPrint.Entitys;

namespace TemplatesPrint.Views
{
    public partial class FrmCreateZDL : UIEditForm
    {
        SchemeEntity _schemeEntity = null; string _schemeType = "";
        public FrmCreateZDL(string schemeType,  SchemeEntity schemeEntity=null)
        {
            InitializeComponent();
            _schemeType = schemeType;
            _schemeEntity = schemeEntity;
        }

        private void FrmCreateZDL_Load(object sender, EventArgs e)
        {
            LoadInfo();
            this.ButtonOkClick += FrmCreateZDL_ButtonOkClick;
        }

        void LoadInfo()
        {
            LoadSchemeAmounts();
            if (_schemeEntity!=null)
            {
                this.Text = "编辑";
                LoadEditInfo();
            }
        }

        void LoadEditInfo()
        {
            edtName.Text = _schemeEntity.xName;
            edtAddress.Text = _schemeEntity.Address;
            dpJoiningTime.Value = _schemeEntity.JoiningTime.Value;


            edtTwoDirector.Text = _schemeEntity.TwoDirector;
            edtTwoTeacher.Text = _schemeEntity.TwoTeacher;
            edtDirectPushTeacher.Text = _schemeEntity.DirectPushTeacher;
            edtMemo.Text = _schemeEntity.Memo;
        }

        void LoadSchemeAmounts()
        {
            using (var db = SQLiteHelper.GetInstance())
            {
                var schemeAmounts = db.Queryable<SchemeAmountEntity>().ToList();
                cbJoiningScheme.ValueMember = "Id";
                cbJoiningScheme.DisplayMember = "JoiningScheme";
                cbJoiningScheme.DataSource = schemeAmounts;
                if (_schemeEntity != null)
                {
                    var joiningScheme=schemeAmounts.Where(e=>e.Id==_schemeEntity.JoiningSchemeId).FirstOrDefault();
                    cbJoiningScheme.SelectedItem = joiningScheme;
                }
            }
        }

        private void FrmCreateZDL_ButtonOkClick(object sender, EventArgs e)
        {
            var schemeAmount = cbJoiningScheme.SelectedItem as SchemeAmountEntity;
            using (var db = SQLiteHelper.GetInstance())
            {
                SchemeEntity schemeEntity = new SchemeEntity();
                schemeEntity.xName = edtName.Text.Trim();
                schemeEntity.Address = edtAddress.Text.Trim();
                schemeEntity.JoiningTime = dpJoiningTime.Value;

                schemeEntity.JoiningSchemeId = schemeAmount.Id;
                schemeEntity.JoiningScheme = schemeAmount.JoiningScheme;
                schemeEntity.Amount = schemeAmount.Amount;
                schemeEntity.TwoDirector = edtTwoDirector.Text.Trim();
                schemeEntity.TwoTeacher = edtTwoTeacher.Text.Trim();
                schemeEntity.DirectPushTeacher = edtDirectPushTeacher.Text.Trim();
                schemeEntity.SchemeType = _schemeType;
                schemeEntity.Memo = edtMemo.Text.Trim();

                schemeEntity.QryInfo = $"{edtName.Text.Trim()}=={edtTwoDirector.Text.Trim()}=={edtTwoTeacher.Text.Trim()}=={edtDirectPushTeacher.Text.Trim()}";
                if (_schemeEntity != null)
                {
                    schemeEntity.Id = _schemeEntity.Id;
                    db.Updateable(schemeEntity).ExecuteCommand();
                }
                else
                {
                    db.Insertable(schemeEntity).ExecuteCommand();
                }
            }
            this.Close();
        }

        protected override bool CheckData()
        {
            return true;
            //CheckEmpty(edtName, "请输入姓名")
            //       && CheckEmpty(edtAddress, "请输入地址")
            //       && CheckEmpty(cbJoiningScheme, "请选择加盟方案")
            //       && CheckEmpty(edtTwoTeacher, "请输入二代")
            //       && CheckEmpty(edtTwoDirector, "请输入理事")
            //       && CheckEmpty(edtDirectPushTeacher, "请输入直推");
        }

        private void cbJoiningScheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            var schemeAmount = cbJoiningScheme.SelectedItem as SchemeAmountEntity;
            lblAmount.Text = schemeAmount.Amount.ToString();
        }
    }
}
